<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
  .main{
    width: 100%;
    height: 1000vh;
    display: flex;
    margin: auto;
  }
  .le{
    width: 50%;
    height: 100%;
    text-align: center;

    background-color: #F2E6CE;
  }
  .rg{
    width: 50%;
    height: 100%;
    text-align: center;
    background-color: #6E8B74;

  }

</style>
<body>
  <div class="main">
    const Lsh = require('ml-lsh');
    const data = [
      { user: 'Alice', item: 'item1', rating: 4 },
      { user: 'Alice', item: 'item2', rating: 3 },
      { user: 'Bob', item: 'item1', rating: 5 },
      { user: 'Bob', item: 'item3', rating: 1 },
      { user: 'Charlie', item: 'item2', rating: 2 },
      { user: 'Charlie', item: 'item3', rating: 4 }
    ];
    const lsh = new Lsh({ numHashes: 2, numBands: 1 });
    const userItems = {};
    data.forEach(({ user, item, rating }) => {
      if (!userItems[user]) userItems[user] = {};
      userItems[user][item] = rating;
    });
    const users = Object.keys(userItems);
    const items = Array.from(new Set(data.map(({ item }) => item)));
    const itemVectors = items.map(item => users.map(user => userItems[user][item] || 0));
    lsh.insert(itemVectors);
    const query = users.map(user => items.map(item => userItems[user][item] || 0));
    const neighbors = lsh.query(query, { threshold: 0.5 });
    const userRecommendations = {};
    users.forEach((user, i) => {
      const userItems = Object.keys(userItems[user]);
      const similarUsers = neighbors[i];
      const similarItems = similarUsers.reduce((items, userIndex) => {
        const otherUser = users[userIndex];
        return items.concat(Object.keys(userItems[otherUser]).filter(item => !userItems.includes(item)));
      }, []);
      const itemRatings = similarItems.reduce((ratings, item) => {
        const itemVector = itemVectors[items.indexOf(item)];
        const rating = itemVector.reduce((sum, vector, j) => sum + vector * query[i][j], 0);
        return Object.assign(ratings, { [item]: rating });
      }, {});
      userRecommendations[user] = Object.keys(itemRatings).sort((a, b) => itemRatings[b] - itemRatings[a]);
    });
    console.log(userRecommendations);
    
  </div>
       
</body>
</html>